﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;

namespace BlogDH.User
{
    public partial class Index : BasePage
    {
        protected string errEmail = string.Empty;
        protected string errArea = string.Empty;
        protected string errPhone = string.Empty;
        protected string shengStr = string.Empty;
        protected string shiStr = string.Empty;
        protected string xianStr = string.Empty;
        protected Model.UserInfo userinfo = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetUserinfo();
            }
            if (Request.HttpMethod == "POST")
            {
                SaveInfo();
            }
        }

        #region 获取地区
        protected string GetSheng()
        {
            List<Model.Area> list = new BLL.Area().GetModelList(" PID=0");
            System.Text.StringBuilder builder = new System.Text.StringBuilder();

            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].AreaID == userinfo.Sheng)
                    builder.Append(string.Format("<option value='{0}' selected='selected'>{1}</option>", list[i].AreaID, list[i].Name));
                else
                    builder.Append(string.Format("<option value='{0}'>{1}</option>", list[i].AreaID, list[i].Name));
            }

            return builder.ToString();
        }

        protected string GetShi()
        {
            System.Text.StringBuilder builder = new System.Text.StringBuilder();
            if (userinfo.Sheng != null && userinfo.Sheng != 0)
            {
                //直辖市判断
                string sql = string.Empty;
                if (userinfo.Sheng == 11 || userinfo.Sheng == 12 || userinfo.Sheng == 31 || userinfo.Sheng == 50)
                { sql = "pid=0 and areaid=" + userinfo.Sheng; }
                else
                { sql = " pid=" + userinfo.Sheng; }

                List<Model.Area> list = new BLL.Area().GetModelList(sql);

                for (int i = 0; i < list.Count; i++)
                {
                    if (list[i].AreaID == userinfo.Shi)
                        builder.Append(string.Format("<option value='{0}' selected='selected'>{1}</option>", list[i].AreaID, list[i].Name));
                    else
                        builder.Append(string.Format("<option value='{0}'>{1}</option>", list[i].AreaID, list[i].Name));
                }
            }
            return builder.ToString();
        }

        protected string GetXian()
        {
            System.Text.StringBuilder builder = new System.Text.StringBuilder();
            if (userinfo.Shi != null && userinfo.Shi != 0)
            {
                List<Model.Area> list = new BLL.Area().GetModelList(" PID=" + userinfo.Shi);

                for (int i = 0; i < list.Count; i++)
                {
                    if (list[i].AreaID == userinfo.Xian)
                        builder.Append(string.Format("<option value='{0}' selected='selected'>{1}</option>", list[i].AreaID, list[i].Name));
                    else
                        builder.Append(string.Format("<option value='{0}'>{1}</option>", list[i].AreaID, list[i].Name));
                }
            }
            return builder.ToString();
        }
        #endregion

        private void GetUserinfo()
        {
            if (userinfo == null)
                userinfo = new BLL.UserInfo().GetModel(UserID, UserName);
            if (userinfo == null)
            {
                Response.Write("<script>window.location.href='/login.aspx'</script>");
                Response.End();
            }
        }

        private void SaveInfo()
        {
            bool flag = true;
            string email = Request.Form["email"];
            string phone = Request.Form["phone"];
            string areaid1 = Request.Form["areaid1"];
            string areaid2 = Request.Form["areaid2"];
            string areaid3 = Request.Form["areaid3"];

            if (string.IsNullOrEmpty(email))
            {
                flag = false;
                errEmail = "*";
            }
            else if (!Regex.IsMatch(email, "([a-zA-Z0-9_\\.\\-])+\\@(([a-zA-Z0-9\\-])+\\.)+([a-zA-Z0-9]{2,4})+"))
            {
                flag = false;
                errEmail = "邮箱格式错误";
            }

            if (string.IsNullOrEmpty(phone))
            {
                flag = false;
                errPhone = "*";
            }
            else if (!Regex.IsMatch(phone, "0?(13[0-9]|15[012356789]|18[0236789]|14[57])[0-9]{8}"))
            {
                flag = false;
                errPhone = "手机号格式错误";
            }

            int sheng = 0;
            int shi = 0;
            int xian = 0;
            int.TryParse(areaid1, out sheng);
            int.TryParse(areaid2, out shi);
            int.TryParse(areaid3, out xian);

            if (!(sheng > 0 && shi > 0 && xian > 0 && xian > shi && xian > sheng && shi >= sheng))
            {
                flag = false;
                errArea = "*";
            }

            if (flag)
            {
                //保存用户信息
                Model.UserInfo user = new Model.UserInfo()
                {
                    UserID = base.UserID,
                    UserName = base.UserName,
                    email = email,
                    phone = phone,
                    Sheng = sheng,
                    Shi = shi,
                    Xian = xian
                };

                flag = new BLL.UserInfo().Update(user);

                //保存博客信息
                if (flag)
                {
                    flag = new BLL.BlogInfo().Update(new Model.BlogInfo() { Userid = base.UserID, BlogUrl = base.UserName, Sheng = sheng, Shi = shi, Xian = xian });
                }

                if (flag)
                {
                    userinfo.email = user.email;
                    userinfo.phone = user.phone;
                    userinfo.Sheng = user.Sheng;
                    userinfo.Shi = user.Shi;
                    userinfo.Xian = user.Xian;
                    Response.Write("<script>alert('保存成功')</script>");
                }
                else
                {
                    Response.Write("<script>alert('保存失败')</script>");
                }
            }
        }
    }
}